package com.wzh.live.controller;

import java.util.Arrays;
import java.util.Map;

import com.wzh.common.utils.JwtUtil;
import com.wzh.live.dao.LiveGoodsDao;
import com.wzh.live.entity.LiveGoodsEntity;
import com.wzh.live.service.LiveGoodsService;
import io.jsonwebtoken.Claims;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wzh.live.entity.OrderLiveEntity;
import com.wzh.live.service.OrderLiveService;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author wzh
 * @email 1281204121@qq.com
 * @date 2022-03-14 14:10:52
 */
@RestController
@RequestMapping("live/orderlive")
public class OrderLiveController {
    @Autowired
    private OrderLiveService orderLiveService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private LiveGoodsDao liveGoodsDao;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,HttpServletRequest request){
        String token = request.getHeader("token");
        if (token!=null){
            Claims claims = jwtUtil.parseJWT(token);
            if (claims!=null&&"admin".equals(claims.get("roles"))){
                params.put("userId",claims.getId());
                PageUtils page = orderLiveService.queryPage(params);
                return R.ok().put("page", page);
            }
            return R.error("未登录");
        }
       return R.error("未登录");
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		OrderLiveEntity orderLive = orderLiveService.getById(id);

        return R.ok().put("orderLive", orderLive);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody OrderLiveEntity orderLive, HttpServletRequest request){
        String token = request.getHeader("token");
        if (token!=null){
            Claims claims = jwtUtil.parseJWT(token);
            if (claims!=null&&"admin".equals(claims.get("roles"))) {
                orderLive.setUserId(Long.valueOf(claims.getId()));
                //减库存
                LiveGoodsEntity byLiveIdAndGoodsId = liveGoodsDao.getByLiveIdAndGoodsId(orderLive.getActivityId(), orderLive.getGoodsId());
                byLiveIdAndGoodsId.setStockCount(byLiveIdAndGoodsId.getStockCount()-orderLive.getGoodsCount());
                Integer i=liveGoodsDao.updStockById(orderLive.getActivityId(), orderLive.getGoodsId(),byLiveIdAndGoodsId.getStockCount());
                if (i>0){
                    //异步下订单
                    rabbitTemplate.convertAndSend("msg4",orderLive);

                    return R.ok("购买成功");
                }
                R.error("购买失败");
            }
            return R.error("未登录");
        }
        return R.error("未登录");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody OrderLiveEntity orderLive){
		orderLiveService.updateById(orderLive);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		orderLiveService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
